<template>
	<view class="">
		<view class="content wrap">
			<u-image :showLoading="true" :src="src" width="250px" height="200px"></u-image>
			<u-form :model="form" ref="uForm"  :rules="rules">
				<u-form-item label="重量*" label-width="150" prop="weight"><u-input v-model="form.weight" type="digit" placeholder="请输入重量"/></u-form-item>
				
				<u-form-item label="采摘人编码" label-width="200" left-icon="account">
					<u-input v-model="form.userNumber" placeholder="请输入采摘人编码"/>
					<u-icon @click="scan()" slot="right" name="scan" size="60"></u-icon>
				</u-form-item>
			</u-form>
			<u-button @click="submit">提交</u-button>
		</view>
		<view>
			<u-toast ref="uToast" />
		</view>
	</view>
</template>
<script>
	export default {
		data() {
			return {
				flex:'',
				src:'https://s1.ax1x.com/2022/06/28/jZtJs0.png',
				form:{
				    times:'',
				    userNumber:"",
					warehouseName:"",
				    goodsName:"",
				    weight:"",
				},
				pick_zf:{},
			    rules: {
					weight: [
						{ 
							required: true,
							message: '请输入重量', 
							trigger: 'blur',
						}
					],
				}
			};
		},
		onLoad(open) {
			//#ifdef H5 || APP-PLUS
			this.flex = 'u-flex'
			//#endif
			
			this.pick_zf = uni.getStorageSync('pick_zf')
			this.form.times =  this.pick_zf.times
			this.form.warehouseName =  this.pick_zf.warehouseName
			this.form.goodsName =  this.pick_zf.goodsName
		},
		onReady() {
			this.$refs.uForm.setRules(this.rules);
		},
		methods:{
			scan(){
				let that = this
				uni.scanCode({
					success: function (res) {
						console.log('条码类型：' + res.scanType);
						console.log('条码内容：' + res.result);
						that.form.userNumber = res.result
						if(typeof that.form.weight != 'undefined' && that.form.weight != null && that.form.weight != ""){
							that.submit()
						}
					}
				});
			},
			submit() {
				let that = this
				this.$refs.uForm.validate(valid => {
					if (valid) {
						console.log('验证通过');
						if(typeof that.form.goodsName == 'undefined' || that.form.goodsName == null || that.form.goodsName == ""){
							uni.showToast({
								title: "未获取到商品",
								icon: 'none'
							})
							return
						}
						uni.showLoading({
							title: '正在提交'
						})
						this.$http.post('/rest/fruits/addItem',{
						},{
							params: that.form,
							header: {
								'Authorization' :uni.getStorageSync('token'),
							}
						}).then(res => {
							uni.hideLoading()
							if(res.data.code == 200) {
								that.form.weight = ''
								this.$refs.uToast.show({
									title: '操作成功',
									type: 'success'
								})
							} else {
								this.$refs.uToast.show({
									title: res.data.message,
									type: 'error'
								})
							}
						})
					} else {
						console.log('验证失败');
					}
				});
			},
		}
	}
</script>
<style lang="scss">
	page{
			background-color: #ffffff;
	}
	.wrap {
		padding: 30rpx;
	}
	.weikaishi2{
		font-size: 36rpx;
	}
	.weikaishi3{
		font-size: 32rpx;
	}
	.huanhang{
		word-break:break-all;
	}
</style>
